<template>
  <a-card :bordered="false" style="padding-bottom: 24px;min-height:600px;">
    <j-form-container :disabled="disabled" class="table-page-search-wrapper">
      <a-form-model
        :layout="layout"
        ref="formRef"
        slot="detail"
        :model="model"
        :rules="validatorRules"
        v-bind="formItemLayout"
      >
        <z-form-item :datas="fieldArr" :form="model" :rules="validatorRules" :gutter="gutter" @change="getChangeTime">
          <!--//自定义姓名-->
          <template slot="slots-personNameOtherAssistance">
            <a-space :size="6" v-if="handleType!=='details'">
              <a-input v-model="model['personName']" placeholder="请输入" :disabled="true" style="width: 412px" />
              <a-button type="primary" @click="onSelect">请选择</a-button>
            </a-space>
            <a-input v-if="handleType==='details'" v-model="model['personName']" placeholder="请输入" :disabled="true" />
          </template>
          <a-col :span="20" :offset="4" slot="submit-action" style="margin-top: 30px" v-if="handleType!=='details'">
            <a-space :size="6">
              <a-button @click="onResetForm">取消</a-button>
              <a-button type="primary" @click="onSubmit">保存</a-button>
            </a-space>
          </a-col>
        </z-form-item>
      </a-form-model>
    </j-form-container>
    <person-list-modal ref="personModal" @ok="getModel" />
  </a-card>
</template>

<script>
import * as serviceApi from '@/api/sjsk/clinicmanageService'
import { underLine2CamelCase, camelCaseUnderLine } from '@/utils/util'
import personListModal from './modules/personListModal'
export default {
  inject:['closeCurrent'],
  components: { personListModal },
  data() {
    return {
      id: null,
      name: null,
      handleType: null,
      confirmLoading: false,
      model: {},
      fieldArr_depositCollection: [
        {label: '姓名', prop: 'personName', tagType: 'input'},
        {label: '性别', prop: 'sex', tagType: 'dict-select', dictCode:'sex'},
        {label: '身份证号', prop: 'idNo', tagType: 'inputr'},
        {label: '出生日期', prop: 'birthDate', tagType: 'date-picker', format: 'YYYY-MM-DD'},
        {label: '联系电话', prop: 'contactPhone', tagType: 'input'},
        {label: '人员类型', prop: 'personType', tagType: 'dict-select', dictCode:'risk_level_person_type'},
        {label: '个人社保编号', prop: 'socialSecurityNumber', tagType: 'input'},
        {label: '失业待遇编号', prop: 'joblessTreatmentNumber', tagType: 'input'},
        {label: '户口性质', prop: 'accountType', tagType: 'input'},
        {label: '原单位名称', prop: 'preUnitNumber', tagType: 'input'},
        {label: '单位社保编号', prop: 'unitSsn', tagType: 'input'},
        {label: '原单位类型', prop: 'preUnitType', tagType: 'input'},
        {label: '失业日期', prop: 'dateOfUnemployment', tagType: 'input'},
        {label: '失业登记日期', prop: 'dateOfUnemploymentRecord', tagType: 'date-picker'},
        {label: '就业失业登记证号', prop: 'cwssl', tagType: 'input'},//
        {label: '银行行别', prop: 'bankType', tagType: 'input'},
        {label: '户籍地址', prop: 'birthLocation', tagType: 'input'},
        {label: '居住地址', prop: 'permanentLocation', tagType: 'input'},
        {label: '银行账号', prop: 'bankAccount', tagType: 'input'},
        {label: '原银行账号', prop: 'preBankAccount', tagType: 'input'},
        {label: '失业保险金', prop: 'unemployedInsuranceGold', tagType: 'input'},
        {label: '待遇核定日期', prop: 'treatmentApprovalDate', tagType: 'date-picker'},
        {label: '待遇开始时间', prop: 'treatmentBeginDate', tagType: 'date-picker'},
        {label: '待遇终止时间', prop: 'treatmentEndDate', tagType: 'date-picker'},
        {label: '应享受月数', prop: 'shouldEnjoyMonth', tagType: 'input'},
        {label: '已享受月数', prop: 'alreadyEnjoyMonth', tagType: 'input'},
        {label: '本次剩余可享受月数', prop: 'remainEnjoyMonth', tagType: 'input'},
        {label: '结转剩余可享受月数', prop: 'remainEnjoyMonth', tagType: 'input'},
        {label: '有效标志', prop: 'enabledFlag', tagType: 'input'},
        {label: '待遇性质', prop: 'treatmentType', tagType: 'input'},
        {label: '待遇变更类型', prop: 'treatmentChangeType', tagType: 'input'},
        {label: '应支年月', prop: 'shouldPayDate', tagType: 'input'},
        {label: '补扣发金额', prop: 'supplementaryAmount', tagType: 'input'},
        {label: '待遇发放标志', prop: 'treatmentSupplyFlag', tagType: 'input'},
        {label: '减少所属期', prop: 'reduceBelongTerm', tagType: 'input'},
        {label: '停发原因', prop: 'stopSupplyReason', tagType: 'input'},
        {label: '恢复原因', prop: 'reSupplyReason', tagType: 'input'},
        {label: '待遇停发/恢复时间', prop: 'treatmentSupplyChangeDate', tagType: 'date-picker'},
        {label: '是否已经转移', prop: 'isTransferredFlag', tagType: 'dict-select', dictCode:'sex'},
        {label: '备注', prop: 'contentComment', tagType: 'input'},
        {label: '数据来源', prop: 'datasource', tagType: 'input'},
        {label: '当前环节', prop: 'currentFlow', tagType: 'input'},
        {label: '经办人', prop: 'applyPerson', tagType: 'input'},
        {label: '经办机构', prop: 'applyOrg', tagType: 'input'},
        {label: '经办时间', prop: 'applyDate', tagType: 'date-picker'},
      ],
      fieldArr_socialSecuritySubsidy: [
        {label: '姓名', prop: 'personName', tagType: 'input'},
        {label: '性别', prop: 'sex', tagType: 'dict-select', dictCode:'sex'},
        {label: '身份证号', prop: 'idNo', tagType: 'input'},
        {label: '出生日期', prop: 'birthDate', tagType: 'date-picker', format: 'YYYY-MM-DD'},
        {label: '联系电话', prop: 'contactPhone', tagType: 'input'},
        {label: '人员类型', prop: 'personType', tagType: 'dict-select', dictCode:'risk_level_person_type'},
        {label: '单位名称', prop: 'unitName', tagType: 'input'},
        {label: '单位社保编号', prop: 'unitSsn', tagType: 'input'},
        {label: '开户银行', prop: 'bank', tagType: 'input'},
        {label: '社保卡账号', prop: 'ssnCardAccount', tagType: 'input'},
        {label: '补贴起始年月', prop: 'beginDateOfSubsidies', tagType: 'date-picker'},
        {label: '补贴终止年月', prop: 'endDateOfSubsidies', tagType: 'date-picker'},
        {label: '补贴月数', prop: 'monthsOfSubsidies', tagType: 'input'},
        {label: '补贴金额', prop: 'amountOfSubsidies', tagType: 'input'},
        {label: '备注', prop: 'contentComment', tagType: 'input'},
        {label: '户籍地址', prop: 'birthLocation', tagType: 'input'},
        {label: '居住地址', prop: 'permanentLocation', tagType: 'input'},
        {label: '经办人', prop: 'applyPerson', tagType: 'input'},
        {label: '经办机构', prop: 'applyOrg', tagType: 'input'},
        {label: '经办时间', prop: 'applyDate', tagType: 'date-picker'},
      ],
      fieldArr_ridPoor: [
        {label: '姓名', prop: 'personName', tagType: 'input'},
        {label: '性别', prop: 'sex', tagType: 'dict-select', dictCode:'sex'},
        {label: '身份证号', prop: 'idNo', tagType: 'input'},
        {label: '出生日期', prop: 'birthDate', tagType: 'date-picker', format: 'YYYY-MM-DD'},
        {label: '联系电话', prop: 'contactPhone', tagType: 'input'},
        {label: '人员类型', prop: 'personType', tagType: 'dict-select', dictCode:'risk_level_person_type'},
        {label: '文化程度', prop: 'degree', tagType: 'dict-select', dictCode:'sex'},
        {label: '状态', prop: 'statusText', tagType: 'dict-select', dictCode:'sex'},
        {label: '家庭成员人数', prop: 'countFamilies', tagType: 'input'},
        {label: '户籍地详址', prop: 'birthAddress', tagType: 'input'},
        {label: '现居住地详址', prop: 'permanentAddress', tagType: 'input'}
      ],
      fieldArr_subsistenceAssistance: [
        {label: '姓名', prop: 'personName', tagType: 'input'},
        {label: '性别', prop: 'sex', tagType: 'dict-select', dictCode:'sex'},
        {label: '身份证号', prop: 'idNo', tagType: 'input'},
        {label: '出生日期', prop: 'birthDate', tagType: 'date-picker', format: 'YYYY-MM-DD'},
        {label: '联系电话', prop: 'contactPhone', tagType: 'input'},
        {label: '人员类型', prop: 'personType', tagType: 'dict-select', dictCode:'risk_level_person_type'},
        {label: '文化程度', prop: 'degree', tagType: 'dict-select', dictCode:'sex'},
        {label: '状态', prop: 'statusText', tagType: 'dict-select', dictCode:'sex'},
        {label: '家庭成员人数', prop: 'countFamilies', tagType: 'input'},
        {label: '户籍地详址', prop: 'birthAddress', tagType: 'input'},
        {label: '现居住地详址', prop: 'permanentAddress', tagType: 'input'},
        {label: '纳入时间', prop: 'joinDate', tagType: 'date-picker'},
        {label: '发放低保金额（元）', prop: 'supplyAmount', tagType: 'input'},
        {label: '发放低保金额（元）', prop: 'supplyAmount2', tagType: 'input'},
        {label: '合计发放低保金额（元）', prop: 'totalSupplyAmount', tagType: 'input'},
      ],
      fieldArr_temporaryAssistance: [
        {label: '姓名', prop: 'personName', tagType: 'input'},
        {label: '性别', prop: 'sex', tagType: 'dict-select', dictCode:'sex'},
        {label: '身份证号', prop: 'idNo', tagType: 'input'},
        {label: '出生日期', prop: 'birthDate', tagType: 'date-picker', format: 'YYYY-MM-DD'},
        {label: '联系电话', prop: 'contactPhone', tagType: 'input'},
        {label: '人员类型', prop: 'personType', tagType: 'dict-select', dictCode:'risk_level_person_type'},
        {label: '文化程度', prop: 'degree', tagType: 'dict-select', dictCode:'sex'},
        {label: '状态', prop: 'statusText', tagType: 'dict-select', dictCode:'sex'},
        {label: '家庭成员人数', prop: 'countFamilies', tagType: 'input'},
        {label: '户籍地详址', prop: 'birthAddress', tagType: 'input'},
        {label: '现居住地详址', prop: 'permanentAddress', tagType: 'input'},
        {label: '救助时间', prop: 'rescueDate', tagType: 'range-picker', format: 'YYYY-MM-DD'},
        {label: '发放救助金（元）', prop: 'supplyAmount', tagType: 'input'},
        {label: '发放救助金（元）', prop: 'supplyAmount2', tagType: 'input'},
        {label: '合计发放金（元）', prop: 'totalAmount', tagType: 'input'},
      ],
      fieldArr_otherAssistance: [
        {label: '吸毒人员姓名', prop: 'personNameOtherAssistance'},
        {label: '性别', prop: 'personSex', tagType: 'dict-select', dictCode:'sex', disabled: true},
        {label: '身份证号', prop: 'identityNumber', tagType: 'input', disabled: true},
        {label: '联系电话', prop: 'phoneNumber', tagType: 'input', disabled: true},
        {label: '人员类型', prop: 'personType', tagType: 'dict-select', dictCode:'risk_level_person_type', disabled: true},
        {label: '户籍地详址', prop: 'birthAddress', tagType: 'input', disabled: true},
        {label: '现居住地详址', prop: 'permanentAddress', tagType: 'input', disabled: true},
        {label: '救助类型', prop: 'rescueType', tagType: 'dict-select', dictOptions: []},
        {label: '救助时间', prop: 'rescueDate', tagType: 'range-picker', format: 'YYYY-MM-DD'},
        {label: '救助状态', prop: 'rescueStatus', tagType: 'dict-select', dictOptions: []},
        {label: '救助计划', prop: 'contentHelpPlan', tagType: 'textarea'},
        {label: '落实情况', prop: 'contentImplStatus', tagType: 'textarea'},
      ],
      rescueTypes: [],
      rescueStatus: [
        { text: '未开始', value: '1' },
        { text: '进行中', value: '2' },
        { text: '已完成', value: '3' },
        { text: '停止', value: '4' },
      ],
      validatorRules: {
        rescueType:[ { required: true, message: '请选择救助类型!' }],
        rescueDate:[ { required: true, message: '请选择救助时间!' }],
        contentHelpPlan:[ { required: true, message: '请输入救助计划!' }],
        contentImplStatus:[ { required: true, message: '请填入落实情况!' },]
      },
    }
  },
  computed: {
    layout() {
      let layout = this.handleType==='details'?'inline':'horizontal'
      return layout
    },
    formItemLayout() {
      let formItemLayout = this.handleType==='details'? {}: { labelCol: { span: 6 }, wrapperCol: { span: 11 } }
      return formItemLayout
    },
    gutter() {
      let gutter = 0
      if (this.handleType && this.handleType==='details') gutter = 48
      return gutter
    },
    disabled() {
      let disabled = this.handleType==='details'? true: false
      return disabled
    },
    fieldArr() {
      let fieldArr = []
      if (this.name) fieldArr = this[`fieldArr_`+this.name]
      if (this.handleType==='details') {
        fieldArr.forEach(item => {
          item.span = 8;
          item.disabled = true;
        })
      } else if (this.handleType!=='details') {
        let fields = ['personSex', 'identityNumber', 'phoneNumber', 'personType', 'birthAddress', 'permanentAddress']
        fieldArr.forEach((item, i) => {
          item.span = 24;
          if (fields.includes(item.prop)===false) {
            item.disabled = false;
          }
        })
      }
      return fieldArr
    },
  },
  created() {
    this.getRouteQuery(this.$route)
  },
  watch: {
    $route(data) {
      this.getRouteQuery(data)
    },
    id(value) {
      this.getRescueDetail()
    }
  },
  mounted() {},
  methods: {
    // 详情信息
    getRescueDetail() {
      serviceApi.getRescueDetail(this.id).then(res => {
        if (res.success) {
          const { result } = res
          this.model = result
          if(result.beginTime && result.endTime) {
            let datas = [result.beginTime, result.endTime]
            console.log(datas)
            this.$set(this.model, 'rescueDate', datas)
          }
        }
      })
    },
    onSelect() {
      this.$refs.personModal.show()
    },
    onSubmit () {
      const parameter = Object.assign({}, this.model)
      this.$refs.formRef.validate((valid)=> {
        if (valid) {
          this.confirmLoading = true
          let api = this.handleType ==='edit'? serviceApi.updateRescue(parameter): serviceApi.saveRescue(parameter)
          api.then(res => {
            this.confirmLoading = false
            if (res.success) {
              this.$message.success(res.message? res.message: '操作成功')
              this.handleCloseCurrentPage()
            } else {
              this.$message.warn(res.message? res.message: '操作失败')
            }
          })
          .catch(e => {
            this.$message.warn('操作失败！')
            this.confirmLoading = false
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    onResetForm() {
      this.handleCloseCurrentPage()
    },
    // 获取组件的时间， prop为对应的字段，date为选中的时间值
    getChangeTime(prop, date) {
      if(prop === 'rescueDate') {
        this.$set( this.model, 'beginTime', date[0] )
        this.$set( this.model, 'endTime', date[1] )
      }
    },
    getModel(value) {
      this.model = Object.assign({}, value, this.model)
    },
    // 监听路由变化
    getRouteQuery($route) {
      this.initTabTypes()
      if (this.$route.params && Object.keys(this.$route.params).length > 0) {
        if(this.$route.params.name) this.name = this.$route.params.name
        if(this.$route.params.id) this.id = this.$route.params.id
        if(this.$route.name) {
          this.handleType = this.$route.name === 'help-rescue-@name-details-@id'? 'details': this.$route.name === 'help-rescue-@name-edit-@id'? 'edit':'add'
        }
      }
    },
    handleCloseCurrentPage() {
      // 注意：以下代码必须存在
      // inject:['closeCurrent'],
      this.closeCurrent()
      this.$router.push({ path: `/help-rescue` })
    },
    // 初始化模型配置项
    initTabTypes() {
      this.$initDictOptions('rescue_type').then((res) => {
        if (res.success) {
          this.rescueTypes = res.result.map((item, index)=> {
            if (index >= 5) {
              return item
            }
          }).filter(obj => obj !== undefined)
          this.fieldArr_otherAssistance.forEach(item => {
            let d = [{ text:'请选择', value:'' }]
            if(item.prop==='rescueType') {
              item.dictOptions = [...d, ...this.rescueTypes]
            }
            if(item.prop==='rescueStatus') {
              item.dictOptions = [...d, ...this.rescueStatus]
            }
          })
        }
      })
    },
  }
}
</script>
<style lang="less" scoped></style>
